//
// Created by yyancy on 2019/11/26.
//

#include <iostream>
#include "Chrono.h"

using namespace std;

Date::Date(int y, Month m, int d)
        : y{y}, m{m}, d{d} {
    if (!is_valid()) throw Invaild{};
}

const Date &default_date() {
    static Date dd{1998, Month::jun, 6};
    return dd;
}

Date::Date()
        : y{default_date().year()},
          m{default_date().month()},
          d{default_date().day()} {

}


void Date::add_day(int n) {

}

void Date::add_year(int n) {
    if (m == Month::feb && d == 29 && !leapyear(y + n)) {
        m = Month::mar;
        d = 1;
    }
    y += n;
}


bool is_date(int y, Month m, int d) {
    if (d <= 0)return false;
    if (m < Month::jan || Month::dec < m)return false;
    int days_in_month = 31;

    switch (m) {
        case Month::feb:
            days_in_month = (leapyear(y)) ? 29 : 28;
            break;
        case Month::apr:
        case Month::jun:
        case Month::sep:
        case Month::nov:
            days_in_month = 30;
            break;
    }

    return days_in_month >= d;
}

bool leapyear(int y) {
    return false;
}

ostream &operator<<(ostream &os, const Date &dd) {
    os << '(' << dd.year()
       << ',' << dd.year()
       << ',' << dd.day() << ')';
}


istream &operator>>(istream &is, Date &dd) {
    int y, m, d;
    char ch1, ch2, ch3, ch4;
    is >> ch1 >> y >> ch2 >> y >> ch3 >> d >> ch4;
    if (!is)return is;

    if (ch1 != '(' || ch2 != ',' || ch3 != ',' || ch4 != ')') {
        is.clear(ios_base::failbit);
        return is;
    }

    dd = Date(y, Month(m), d);
    return is;
}


bool Date::is_valid() {
    if (m < Month::jan || m > Month::dec)return false;
}

void f(Month x, int y) {
    try {
        Date dxy{2004, x, y};
//        std::cout << dxy << '\n';
    } catch (Date::Invaild) {
        error("invalid date");
    }
}


void error(string string1) {
    cerr << string1 << "\n";
    exit(1);
}

Month int_to_month(int x) {
    if (x < int(Month::jan) || x > int(Month::dec))error("bad month");
    return Month(x);
}

Month operator++(Month &m, int abc) {
    m = (m == Month::dec) ?
        Month::jan :
        Month(int(m) + 1);
    return m;
}


int main() {
    f(Month::nov, 14);
    Month m = Month::feb;
    Month mm = Month(2);
    mm++;
    1;

}